package com.example.androidsx;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import java.util.ArrayList;
import java.util.List;

public class AddressActivity extends AppCompatActivity {

    private LinearLayout addressContainer;
    private final List<Address> addressList = new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_page);

        addressContainer = findViewById(R.id.addressContainer);
        Button addButton = findViewById(R.id.btnAddAddress);
        TextView backButton = findViewById(R.id.ymbj);
        backButton.setOnClickListener(v -> {

            finish();
        });
        // 添加示例地址
        addSampleAddresses();

        addButton.setOnClickListener(v -> showAddAddressDialog());
    }

    private void addSampleAddresses() {
        addAddressToLayout(new Address("张小明", "138****1234", "北京市海淀区中关村大街1号科技大厦A座1001室", true));
        addAddressToLayout(new Address("李小红", "139****5678", "上海市浦东新区张江高科技园区科苑路88号", false));
        addAddressToLayout(new Address("王先生", "135****9012", "广东省深圳市南山区科技园南区T2栋B座", false));
    }

    private void showAddAddressDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_address, null);
        builder.setView(dialogView);

        EditText etName = dialogView.findViewById(R.id.etName);
        EditText etPhone = dialogView.findViewById(R.id.etPhone);
        EditText etAddress = dialogView.findViewById(R.id.etAddress);

        AlertDialog dialog = builder.create();
        dialog.show();

        dialogView.findViewById(R.id.btnConfirm).setOnClickListener(v -> {
            String name = etName.getText().toString();
            String phone = etPhone.getText().toString();
            String address = etAddress.getText().toString();

            if (!name.isEmpty() && !phone.isEmpty() && !address.isEmpty()) {
                addAddressToLayout(new Address(name, phone, address, false));
                dialog.dismiss();
            }
        });

        dialogView.findViewById(R.id.btnCancel).setOnClickListener(v -> dialog.dismiss());
    }

    private void addAddressToLayout(Address address) {
        addressList.add(address);
        View addressItem = LayoutInflater.from(this).inflate(R.layout.item_address, addressContainer, false);

        // 设置地址信息
        TextView tvName = addressItem.findViewById(R.id.tvName);
        TextView tvPhone = addressItem.findViewById(R.id.tvPhone);
        TextView tvAddress = addressItem.findViewById(R.id.tvAddress);
        TextView tvDefaultTag = addressItem.findViewById(R.id.tvDefaultTag);
        CheckBox cbDefault = addressItem.findViewById(R.id.cbDefault);

        tvName.setText(address.getName());
        tvPhone.setText(address.getPhone());
        tvAddress.setText(address.getAddress());
        updateDefaultIndicator(tvDefaultTag, cbDefault, address.isDefault());

        // 设置默认地址切换逻辑
        cbDefault.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                // 取消其他地址的默认状态
                for (Address addr : addressList) {
                    if (addr != address) {
                        addr.setDefault(false);
                    }
                }
                address.setDefault(true);
                refreshDefaultIndicators(); // 更新所有地址项的UI
            } else if (address.isDefault()) {
                // 防止取消当前默认地址
                cbDefault.setChecked(true);
            }
        });

        // 删除按钮
        addressItem.findViewById(R.id.btnDelete).setOnClickListener(v -> {
            addressList.remove(address);
            addressContainer.removeView(addressItem);
        });

        // 添加到容器（在"添加按钮"之前）
        addressContainer.addView(addressItem, addressContainer.getChildCount() - 1);
    }

    // 更新单个地址项的默认指示器
    private void updateDefaultIndicator(TextView tagView, CheckBox checkBox, boolean isDefault) {
        tagView.setVisibility(isDefault ? View.VISIBLE : View.GONE);
        checkBox.setChecked(isDefault);
    }

    // 刷新所有地址项的默认状态显示
    private void refreshDefaultIndicators() {
        for (int i = 0; i < addressContainer.getChildCount() - 1; i++) {
            View addressItem = addressContainer.getChildAt(i);
            if (addressItem instanceof CardView) {
                TextView tvDefaultTag = addressItem.findViewById(R.id.tvDefaultTag);
                CheckBox cbDefault = addressItem.findViewById(R.id.cbDefault);

                // 通过地址对象匹配视图状态
                for (Address addr : addressList) {
                    TextView nameView = addressItem.findViewById(R.id.tvName);
                    if (nameView.getText().equals(addr.getName())) {
                        updateDefaultIndicator(tvDefaultTag, cbDefault, addr.isDefault());
                        break;
                    }
                }
            }
        }
    }

    // 地址数据模型
    static class Address {
        private final String name;
        private final String phone;
        private final String address;
        private boolean isDefault;

        public Address(String name, String phone, String address, boolean isDefault) {
            this.name = name;
            this.phone = phone;
            this.address = address;
            this.isDefault = isDefault;
        }

        // Getters & Setters
        public boolean isDefault() { return isDefault; }
        public void setDefault(boolean aDefault) { isDefault = aDefault; }
        public String getName() { return name; }
        public String getPhone() { return phone; }
        public String getAddress() { return address; }
    }
}